﻿using Quartz;
using Quartz.Spi;

namespace QYmall.Core.API.Quartz
{
    /// <summary>
    /// 任务工厂
    /// </summary>
    public class QuartzJobFactory : IJobFactory
    {
        private readonly IServiceProvider _serviceProvider;

        /// <summary>
        /// 依赖注入
        /// </summary>
        public QuartzJobFactory(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        /// <summary>
        /// 返回IJob
        /// </summary>
        public IJob NewJob(TriggerFiredBundle triggerFiredBundle, IScheduler scheduler)
        {
            Type jobType = triggerFiredBundle.JobDetail.JobType;
            IJob? job = _serviceProvider.GetService(jobType) as IJob;
            if (job == null) throw new ArgumentNullException(nameof(job));
            return job;
        }

        /// <summary>
        /// 销毁IJob
        /// </summary>
        public void ReturnJob(IJob job) { }
    }

}
